const commands = {};

export function registerCommand(command, func) {
    if (typeof command !== 'string') {
        throw new Error(`Expected to receive string instead received ${typeof command}`);
    } else if (typeof func !== 'function') {
        throw new Error(`Expected to receive function instead received ${typeof func}`);
    } else if (commands[command]) {
        throw new Error(`A command named ${command} already exists`);
    } else {
        commands[command] = func;
    }
}

export function canExecuteCommand(command) {
    return commands.hasOwnProperty(command);
}

export function executeCommand(command, target, value, options) {
    if (!commands[command]) {
        throw new Error(`The command ${command} is not registered with any plugin`);
    } else {
        return commands[command](target, value, options);
    }
}
